---
title: "Terrorism characterization in Western Europe between 1970 and 2017 "
author: Marie Joigneau - Alexandre Homo - Françoise Le Moal
output:
flexdashboard::flex_dashboard:
theme: journal
social: menu
source_code: embed
---
```{r setup, include = FALSE}
############ ---- LIBRARY ----------------
library(ggplot2)
library(tidyverse)
library(ggsci)
library(leaflet)
library(plotly)
library(egg)
library(ggpubr)
library(patchwork)
library(viridis)
library(scales)
############ ---- IMPORTATION ET PRE TRAITEMENT ----------------
terrorism <- read.csv("terrorism_dataset.csv",sep=";") # on a 181 690 observations
# On supprime les NA sur les variables latitude longitude
idx_NA_lat <- which(is.na(terrorism$latitude)==TRUE)
idx_NA_lon <- which(is.na(terrorism$longitude)==TRUE)
idx_NA <- unique(c(idx_NA_lat,idx_NA_lon))
terrorism <- terrorism[-idx_NA,] # on a 177 133 observations sans NA en latitude et longitude
# On met ensuite en facteur les variables utiles plus tard:
terrorism$attacktype1_txt <- as.factor(terrorism$attacktype1_txt)
levels(terrorism$attacktype1_txt)
terrorism$region_txt <- as.factor(terrorism$region_txt)
levels(terrorism$region_txt)
terrorism$iyear <- as.factor(terrorism$iyear)
levels(terrorism$iyear)
terrorism$country_txt <- as.factor(terrorism$country_txt)
levels(terrorism$country_txt)
# Et on garde uniquement l'Europe de l'Ouest
idx_WE <- which(terrorism$region_txt=="Western Europe")
length(idx_WE)
terrorism_eu <- terrorism[idx_WE,] # on a 21 599 observations en Europe de l'Ouest
############ ---- GRAPHE 1 : CARTE (VISION SPATIALE) ----------------
# On créé différents jeux de données pour les plots :
# En vert : pas de victimes (morts et blessés)
terrorism_eu_nonkill <- terrorism_eu[which(terrorism_eu$nkill==0),]
terrorism_eu_nonwound <- terrorism_eu[which(terrorism_eu$nwound==0),]
# En noir : pas d'information
terrorism_eu_NA_kill <- terrorism[which(is.na(terrorism$nkill)==TRUE),]
terrorism_eu_NA_kill <- terrorism_eu_NA_kill[which(terrorism_eu_NA_kill$region_txt=="Western Europe"),]
terrorism_eu_NA_wound <- terrorism[which(is.na(terrorism$nwound)==TRUE),]
terrorism_eu_NA_wound <- terrorism_eu_NA_wound[which(terrorism_eu_NA_kill$region_txt=="Western Europe"),]
# En rouge : morts
terrorism_eu_kill <- terrorism_eu[which(terrorism_eu$nkill>=1),]
# En bleu : blessés
terrorism_eu_wound <- terrorism_eu[which(terrorism_eu$nwound>=1),]
# On initialise les couleurs et la légende pour la carte
Color_Assets <- as.factor(c("green", "blue", "red", "black"))
info <- as.factor(c("No victim","Wounded","Fatalities", "No information"))
############ ---- GRAPHE 2 : VISION TEMPORELLE ----------------
# On ne garde que les 3 variables attaques, année et pays pour ce graphe :
df1 <- data.frame(attaque=terrorism$eventid,annee=terrorism$iyear,country=terrorism$country_txt)
# Puis on regroupe par année et pays et on compte le nombre d'attaques
df3 <- df1%>%
group_by(annee,country)%>%
count(attaque)
# On fait un data frame pour voir le nombre d'attentat par pays ...
df2 <- as.data.frame.matrix(table(df1$annee,df1$country))
sumjour<-colSums(df2)#;sumjour
# ... et on garde les 4 pays avec le plus d'attentats En Europe:
idx_country <- which((df3$country=="France")|(df3$country=="Italy")|(df3$country=="Spain")|(df3$country=="United Kingdom"))
df3 <- df3[idx_country,]
# On remet bien en facteur l'année
class(df3$annee)
df3$annee <- as.factor(df3$annee)
# Afin d'unifier les 2 derniers graphes entre eux, et avec les annotate et geom_text, on retrouve les noms des couleurs pas défaut: "#F8766D" "#7CAE00" "#00BFC4" "#C77CFF"
color_chosen <- c("red","blue","orange","purple")
for(i in 1:8){
print(hue_pal()(i))
}
# On prépare les carrés gris de fond pour le graphe, un carré gris pour 10 ans, tous les 10 ans
data_breaks <- data.frame(start = c("1970","1990","2010"),
end = c("1980","2000","2017"),
colors = c("gray90","gray90","gray90"))
############ ---- GRAPHE 3 : BARPLOT ----------------
# On ne garde que les 3 variables attaques, type de cible, région et pays :
df4 <- data.frame(attaque=terrorism$eventid,cible=terrorism$targtype1_txt,country=terrorism$country_txt,region=terrorism$region_txt,mort=terrorism$nkill,blesse=terrorism$nwound)
# On se concentre sur les 4 pays d'Europe
df4 <- df4[which(df4$region=="Western Europe"),]
df5 <- df4[which((df4$country=="France")|(df4$country=="Italy")|(df4$country=="Spain")|(df4$country=="United Kingdom")),]
# On ne garde que les colonnes type de cible et pays, pour le barplot :
df5 <- df5[,2:3]
df5$cible <- as.factor(df5$cible)
# On décide de mettre les types de cible qu'on ne voit qu'une ou deux fois dans 'Other' pour des soucis de visualisation
df5[df5 == ""] <- "Other"
df5[df5 == "Abortion Related"] <- "Other"
df5[df5 == "Food or Water Supply"] <- "Other"
df5[df5 == "Maritime"] <- "Other"
df5[df5 == "NGO"] <- "Other"
df5[df5 == "Unknown"] <- "Other"
df5[df5 == "Violent Political Party"] <- "Other"
# On trie le data frame pour des soucis de visualisation :
df5 <- within(df5, cible <- factor(cible, levels=names(sort(table(cible), increasing=TRUE))))
```
Localisation
=======================================================================
### Terrorist attacks in Western Europe between 1970 and 2017
```{r}
# On commence par créer la carte leaflet :
longline_map <- leaflet(terrorism_eu) %>%
setView(lng = mean(terrorism_eu$longitude), lat = mean(terrorism_eu$latitude), zoom = 4) %>%
addTiles()
# Puis on rajoute tous les cercles nécessaires ... :
longline_map %>% addCircles(lng = terrorism_eu_nonkill$longitude, lat = terrorism_eu_nonkill$latitude, weight =(1), radius = 1,
color="green", popup = paste("Number of fatalities =",terrorism_eu_nonkill$nkill," in ",terrorism_eu$iyear, " by ", terrorism_eu$attacktype1_txt)) %>%
addCircles(lng = terrorism_eu_nonwound$longitude, lat = terrorism_eu_nonwound$latitude, weight =(1),radius = 1,
color="green", popup = paste("Number of wounded =",terrorism_eu_nonwound$nwound," in ",terrorism_eu$iyear, " by ", terrorism_eu$attacktype1_txt)) %>%
addCircles(lng = terrorism_eu_NA_kill$longitude, lat = terrorism_eu_NA_kill$latitude, weight=(1),radius = 1,
color="black", popup = paste("Number of fatalities =",terrorism_eu_NA_kill$nkill," in ",terrorism_eu$iyear, " by ", terrorism_eu$attacktype1_txt)) %>%
addCircles(lng = terrorism_eu_NA_wound$longitude, lat = terrorism_eu_NA_wound$latitude, weight=(1),radius = 1,
color="black", popup = paste("Number of wounded =",terrorism_eu_NA_wound$nwound," in ",terrorism_eu$iyear, " by ", terrorism_eu$attacktype1_txt)) %>%
addCircles(lng = terrorism_eu_wound$longitude, lat = terrorism_eu_wound$latitude, weight=1 ,radius = 150*terrorism_eu_wound$nwound,
color= "blue", fillOpacity = 0.1, popup = paste("Number of wounded =",terrorism_eu_wound$nwound," in ",terrorism_eu$iyear, " by ", terrorism_eu$attacktype1_txt)) %>%
addCircles(lng = terrorism_eu_kill$longitude, lat = terrorism_eu_kill$latitude, weight=1, radius = 150*terrorism_eu_kill$nkill,
color="red", popup = paste("Number of fatalities =",terrorism_eu_kill$nkill," in ",terrorism_eu$iyear, " by ", terrorism_eu$attacktype1_txt)) %>%
# ... ainsi que la légende :
addLegend("bottomleft", colors = Color_Assets, labels = info, opacity = 1)
```
Evolution
=======================================================================
### Count of terrorist attacks in 4 Western Europe countries between 1970 and 2017
```{r}
graph2 <- ggplot() +
geom_rect(data=data_breaks,aes(xmin = start,
xmax = end,
ymin = 0,
ymax = 310,
fill = colors),alpha = 0.5) +
geom_line(data=df3, aes(x = annee, y = n, group = country, color = country),size=1) +
annotate("segment", x = "1990", xend = "1997",y = 315, yend = 315,
arrow = arrow(ends = "both", angle = 90),colour = "#F8766D") +
geom_text(aes(x="1993",y=325),label="Islamic groups (GIA)", nudge_x = 0.05, nudge_y = 0.05, check_overlap = T,col="#F8766D")+
annotate("segment", x = "1978", xend = "1996",y = 290, yend = 290,
arrow = arrow(ends = "both", angle = 90),colour = "#F8766D") +
geom_text(aes(x="1987",y=300),label="Corsica terrorism (FLNC)", nudge_x = 0.05, nudge_y = 0.05, check_overlap = T,col="#F8766D")+
annotate("segment", x = "1972", xend = "1980",y = 315, yend = 315,
arrow = arrow(ends = "both", angle = 90),colour = "#7CAE00") +
geom_text(aes(x="1976",y=325),label="Years of Lead", nudge_x = 0.05, nudge_y = 0.05, check_overlap = T,col="#7CAE00")+
annotate("segment", x = "1970", xend = "1998",y = 345, yend = 345,
arrow = arrow(ends = "both", angle = 90),colour = "#C77CFF") +
geom_text(aes(x="1984",y=355),label="The Troubles (IRA)", nudge_x = 0.05, nudge_y = 0.05, check_overlap = T,col="#C77CFF")+
annotate("segment", x = "1970", xend = "2001",y = 375, yend = 375,
arrow = arrow(ends = "both", angle = 90),colour = "#00BFC4") +
geom_text(aes(x="1986",y=385),label="Basque nationalism (ETA)", nudge_x = 0.05, nudge_y = 0.05, check_overlap = T,col="#00BFC4")+
annotate("segment", x = "2004", xend = "2017",y = 315, yend = 315,
arrow = arrow(ends = "both", angle = 90),colour = "grey30") +
geom_text(aes(x="2010",y=325),label="Radical islamic terrorism", nudge_x = 0.05, nudge_y = 0.05, check_overlap = T,col="grey30")+
#scale_color_manual(values=color_chosen)+
#scale_color_jco()+
# then details for presentation (on enlève le titre qui apparaît déjà dans le flexdashboard)
labs(y = "Count of terrorist attacks",x="Year")+
scale_x_discrete(breaks=c("1970","1975","1980","1985","1990","1995","2000","2005","2010","2015")) +
scale_fill_identity()+
theme_bw() +
theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(), panel.background = element_blank(), axis.line = element_line(colour = "black"))+
theme(plot.title = element_text(hjust = 0.5,size=9))+
theme(axis.text.x = element_text(angle = 45, hjust = 1))
#graph2
# on modifie la légende :
plt <- ggplotly(graph2)
plt$x$data[[1]]$name <- " "
plt$x$data[[1]]$showlegend <- FALSE
plt$x$data[[2]]$name <- "France"
plt$x$data[[3]]$name <- "Italy"
plt$x$data[[4]]$name <- "Spain"
plt$x$data[[5]]$name <- "United Kingdom"
plt
```
Distribution
=======================================================================
### Target type histogram depending of country
```{r}
# On met un thème classic
theme_set(theme_classic())
g <- ggplot(df5, aes(cible))+
# On choisit un geom_bar
geom_bar(aes(fill=country), width = 0.5) +
# On inverse x et y pour des soucis de visualisation :
coord_flip()+
# On met des titres aux axes :
labs(y = "Number of attacks",x="Type of target") +
# Thème et visualisation :
theme(axis.text.x = element_text(angle=65, vjust=0.6)) +
theme_bw() +
theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(), panel.background = element_blank(), axis.line = element_line(colour = "black"))
#g
ggplotly(g)
```